package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.common;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.entity.projectile.ProjectileUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.MathHelper;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;

public class RemoteSkill extends BaseSkill {

    public RemoteSkill(int level) {
        super(level);
    }

    public RemoteSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public RemoteSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setCooldown(60);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("远程技能，可以发动额外的远程攻击。随等级提升伤害。冷却时间 %d tick", skillConfig.getCooldown());
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.getTarget() != null && getSkilltime() < mobEntity.world.getTime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown());
            LivingEntity target = mobEntity.getTarget();
            ItemStack itemStack = new ItemStack(Items.ARROW);
            PersistentProjectileEntity persistentProjectileEntity = ProjectileUtil.createArrowProjectile(mobEntity, itemStack, 1);
            persistentProjectileEntity.setPos(persistentProjectileEntity.getX(), persistentProjectileEntity.getY() + 1.5f, persistentProjectileEntity.getZ());
            double d = target.getX() - mobEntity.getX();
            double e = target.getBodyY(0.15) - persistentProjectileEntity.getY();
            double f = target.getZ() - mobEntity.getZ();
            double g = MathHelper.sqrt(d * d + f * f);
            persistentProjectileEntity.setVelocity(d, e + g * (double) 0.2f, f, 1.2f + 0.4f + getSkillLevel(), 14 - (Math.min(getSkillLevel(), 3)) * 4);
            mobEntity.playSound(SoundEvents.ENTITY_SKELETON_SHOOT, 1.0f, 1.0f / (mobEntity.getRandom().nextFloat() * 0.4f + 0.8f));
            mobEntity.world.spawnEntity(persistentProjectileEntity);
        }
        return true;
    }
}
